package com.mall507.fivepeaks.activity.community;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hzh.frame.comn.ItemDecoration.BaseGridSpacingItemDecoration;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.hzh.frame.util.AndroidUtil;
import com.hzh.frame.util.StatusBarUtil;
import com.hzh.frame.widget.xrecyclerview.BaseRecyclerAdapter;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.matisse.GifSizeFilter;
import com.mall507.comn.model.UploadImage;
import com.mall507.comn.tools.UserTools;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.util.ImageUtil;
import com.mall507.fivepeaks.util.UriUtil;
import com.mall507.fivepeaks.util.Util;
import com.mall507.fivepeaks.widget.xdialog.XRadioGroupDialog;
import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropActivity;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.PicassoEngine;
import com.zhihu.matisse.filter.Filter;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Route(path = "/community/CommunityProblemReleaseUI")
public class CommunityProblemReleaseUI extends BaseUI {
    
    @ViewInject(R.id.write)
    EditText write;
    @ViewInject(R.id.content)
    EditText content;
    @ViewInject(R.id.writeNumber)
    TextView writeNumber;
    @ViewInject(R.id.contentNumber)
    TextView contentNumber;
    @ViewInject(R.id.submit) 
    Button submit;
    
    @Override
    protected void onCreateBase() {
        setContentView(R.layout.ui_community_problem_release);
        getTitleView().setContent("发布问题");
        write.setHorizontallyScrolling(true);
        write.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                writeNumber.setText(s.length() + "/7");
            }
        });

        content.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                contentNumber.setText(s.length() + "/99");
            }
        });
        
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitProblem();
            }
        });
    }
    
    protected void submitProblem() {
        JSONObject params = new JSONObject();
        try {
            params.put("title", write.getText().toString());
            params.put("content", content.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (TextUtils.isEmpty(write.getText()) || TextUtils.isEmpty(content.getText())){
            alert("输入标题以及具体内容才可以发布问答");
        }else {
            if (content.getText().toString().length() < 10){
                alert("请描述一下您的具体内容(10至400字之间)");
            }else {
                BaseHttp.getInstance().write("Explorer/postQuestions", params, new HttpCallBack() {
                    @Override
                    public void onSuccess(JSONObject response) {
                        if (response.optInt("code") == 200) {
                            write.getText().clear();
                            content.getText().clear();
                            finish();
                        }
                        alert(response.optString("msg"));
                    }
                });
            }
        }
    }
}
